﻿
--drop function must with save attribute
drop function posTerminalSave(out xml, inout xml);

create or replace function posTerminalSave(
  out xml, inout xml
)  as
$$
  declare
    statusXmlData  alias for $1;
    posTerminalModelXmlData alias for $2;
    vIdposTerminalModel integer := null;
    vNameposTerminalModel text := null;
    vIdNode varchar(100) := '/posterminalmodel/@id';
    vNameposTerminalModelNode varchar(100) := '/posterminalmodel/posterminalmodelname';
  begin
    vIdposTerminalModel := cast(extract_value(vIdNode, cast(posTerminalModelXmlData as xml)) as integer);
    vNameposTerminalModel := extract_value(vNameposTerminalModelNode, cast(posTerminalModelXmlData as xml));
    if (vIdposTerminalModel = null or vIdposTerminalModel = noId()) then
    begin
	insert into posTerminalModel(nameposTerminalModel) values(vNameposTerminalModel) returning idposTerminalModel into vIdposTerminalModel;
	posTerminalModelXmlData := posTerminalModelToXml(vIdposTerminalModel, vNameposTerminalModel);
	statusXmlData := statusCall(successcode(), 'Inserted')
	return;
    end;
    else
    begin 
	update posTerminalModel
	set nameposTerminalModel = vNameposTerminalModel
	where
	  idposTerminalModel = vIdposTerminalModel;
	  statusXmlData := statusCall(successcode(), 'Updated')
	return;
    end;
    end if;
  end;
$$ language plpgsql VOLATILE;




